<?php
/**
 * Description: 取消收藏
 * User: devforma
 * Date: 15/1/22
 * Time: 12:41
 */

class DeleteCollectedTask implements iController{
    
    private $userIdField, $userId;

    /**
     * api请求处理主函数
     * @param array $queryArgs 就是$_REQUEST变量
     * @return array api返回结果，必须含response_code
     */
    public function output($queryArgs) {
        global $uddb;
        
        //验证并确定求求用户的身份，为0表示验证失败，为1表示正式用户，为2表示游客
        $u = validateUser($queryArgs); 
        if ($u == 0 || empty($queryArgs['task_id']) || !is_numeric($queryArgs['task_id'])) {
            return genOutput(1000);
        }
        
        if ($u == 1) { //正式用户
            $this->userIdField = 'user_id';
            $this->userId = $queryArgs['user_id'];
        } elseif ($u == 2) { //游客身份
            $this->userIdField = 'guest_id';
            $this->userId = $queryArgs['guest_id'];
        } else {
            return genOutput(1000);
        }
        
        $query = "SELECT `id` FROM `{$uddb->collection}` WHERE `{$this->userIdField}`=:user
        AND `mission_id`=:task";
        $existId = $uddb->getVar($query, array(':user'=>$this->userId, ':task'=>$queryArgs['task_id']));

        if (!empty($existId)) {
            $uddb->delete($uddb->collection, array('id' => $existId));
            return genOutput(1);
        }
        
        return genOutput(2003);
    }
}